package top.lt.lt_ssm.controller;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.lt.lt_ssm.bean.product;
import top.lt.lt_ssm.service.ProductService;
import top.lt.lt_ssm.util.SimpleResult;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Resource
	private ProductService productService;
	
	@RequestMapping("/getall")
	@ResponseBody
	public SimpleResult GetAll(){
		List<product> ps = productService.getall();
		return new SimpleResult(ps);
	}

	@RequestMapping("/del/{pid}")
	@ResponseBody
	public SimpleResult del(@PathVariable("pid") int id){
		int count = productService.delBypid(id);
		return new SimpleResult(count>0?true:false);
	}
	
	@RequestMapping("/cdgbyID/{pid}")
	public String GetbyID(@PathVariable("pid") int pid,Model model){
		product bp = productService.getByPid(pid);
		model.addAttribute("bp", bp);
		return "product/update";
	}
	
	@RequestMapping("/updbypid")
	public String updBinfo(product pro){
		int pr = productService.updBypid(pro);
		if(pr!=0){
			return "redirect:list";
		}else{
			return "redirect:list";
		}
	}
	@RequestMapping("/GetByid/{pid}")
	public @ResponseBody SimpleResult GetByid(@PathVariable("pid") int sid) {
		List<product> getByid = productService.getByid(sid);
		return new SimpleResult(getByid);
	}
	@RequestMapping("/GetByname/{pname}")
	public @ResponseBody SimpleResult GetByname(@PathVariable("pname") String pname) {
		List<product> getByname= productService.gatByname(pname);
		return new SimpleResult(getByname);
	}
	
	@RequestMapping("/add")
	public String productadd() {
		return "product/add";
	}
	
	@RequestMapping("/Add")
	public String add(product pro){
		int adds = productService.add(pro);
		if(adds!=0){
			return "redirect:list";
		}else{
			return "redirect:list";
		}
	}
	
	@RequestMapping("/list")
	public String productList() {
		return "product/product_list";
	}
}
